// /*
// @auther fenglei
// */
package aliyun_sls

import (
	"fmt"
	sls "github.com/aliyun/aliyun-log-go-sdk"
)

// SlsOptions defines options.
type SlsOptions struct {
	YunAccessKeyId     string
	YunAccessKeySecret string
	IsWan              bool //是否外网访问
	// +optional
	//LogLevel logger.LogLevel
}

func NewSls(opts *SlsOptions) (sls.ClientInterface, error) {
	//参数校验
	if err := setSlsDefaults(opts); err != nil {
		return nil, err
	}
	endpoint := sls_Intranet_Endpoint
	if opts.IsWan {
		endpoint = sls_Endpoint
	}
	credentialsProvider := sls.NewStaticCredentialsProvider(opts.YunAccessKeyId, opts.YunAccessKeySecret, "")
	client := sls.CreateNormalInterfaceV2(endpoint, credentialsProvider)
	_, err := client.ListProject()
	if err != nil {
		return nil, fmt.Errorf("InitYunSls err", err)
	}
	return client, nil
}

func CloseSls(sls sls.ClientInterface) error {
	if sls != nil {
		err := sls.Close()
		if err != nil {
			return err
		}
	}
	return nil
}

func setSlsDefaults(opts *SlsOptions) error {
	if opts.YunAccessKeySecret == "" {
		return fmt.Errorf("YunAccessKeySecret is nil")
	}
	if opts.YunAccessKeyId == "" {
		return fmt.Errorf("YunAccessKeyId is nil")
	}
	return nil
}
